#include "statusManager.h"
#include "log/Logging.h"
#include "common/httpUtil.h"
#include "../param.h"
#include <string>
#include <set>
#include <regex>

using namespace std;

StatusManager* StatusManager::Instance = nullptr;

StatusManager* StatusManager::getInstance(){
    if(Instance == nullptr){
        Instance = new StatusManager();
    }
    return Instance;
}

void StatusManager::updateStatusList(){
    LOG_HLIGHT << "START TO GET STATUSLIST.....";
    std::vector<Json::Value> deviceStatusListVec;
    // std::vector<Json::Value> groupStatusListVec;
    std::set<string> siteNameSet = SiteRecord::getInstance()->getSiteName();
    std::set<string> findSites{BleSiteID, ZigbeeSiteID};
    smatch sm;
    for(auto& elem : siteNameSet){
        if(regex_match(elem, sm, regex("(.*):(.*)"))){
            string uuid = sm.str(1);
            string siteID = sm.str(2);
            if(findSites.find(siteID) != findSites.end()){
                qlibc::QData statusRequest, statusResponse;
                statusRequest.setString("service_id", "get_device_state");
                statusRequest.setValue("request", Json::nullValue);
                SiteRecord::getInstance()->sendRequest2Site(sm.str(0), statusRequest, statusResponse);       //获取状态列表
                if(statusResponse.getInt("code") == 0){
                    //添加来源标签
                    qlibc::QData deviceStatusList =  statusResponse.getData("response").getData("device_list");
                    // qlibc::QData groupStatusList  =  statusResponse.getData("response").getData("group_list");
                    deviceStatusListVec.push_back(addMacSource(deviceStatusList, elem).asValue());
                    // groupStatusListVec.push_back(addMacSource(groupStatusList, elem).asValue());
                }
            }
        }
    }

    //整合设备列表
    qlibc::QData totalDeviceStatusList, totalGroupStatusList;
    for(auto& list : deviceStatusListVec){
        mergeList(list, totalDeviceStatusList);
    }
    // for(auto& list : groupStatusListVec){
    //     mergeList(list, totalGroupStatusList);
    // }
    LOG_BLUE << "GET STATUSLIST COMPLETE.....";
    
   
    statusData.clear();
    statusData.putData("device_list", totalDeviceStatusList);
    // statusData.putData("group_list", totalGroupStatusList);
}

qlibc::QData StatusManager::getStatusDdata()
{
    updateStatusList();
    return statusData;
}

void StatusManager::mergeList(Json::Value& value, qlibc::QData &totalList) {
    qlibc::QData list(value);
    for(Json::ArrayIndex i = 0; i < list.size(); ++i){
        qlibc::QData item = list.getArrayElement(i);
        totalList.append(item);
    }
}


qlibc::QData StatusManager::addMacSource(qlibc::QData& list, string sourceTag){
    Json::ArrayIndex num = list.size();
    qlibc::QData newList;
    for(Json::ArrayIndex i = 0; i < num; ++i){
        qlibc::QData item = list.getArrayElement(i);
        item.setString("sourceSite", sourceTag);    //标记设备来源
        newList.append(item);
    }
    return newList;
}

